<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Events extends CI_Controller {

    private $is_admin, $is_student;

    function __construct() {
        parent::__construct();

        $this->load->model('events_model');
        $this->load->model('classes_model');
        $this->load->model('students_model');
        $this->load->model('user_profiles_model');

        $this->load->library('form_validation');
        $this->load->helper('form');
        $this->lang->load('events');

        $this->is_admin = in_array($this->tank_auth->get_role(), array('teacher', 'student_asso', 'student_repr'));
        $this->is_student = in_array($this->tank_auth->get_role(), array('student', 'student_asso', 'student_repr'));

        if(!$this->is_admin and !$this->is_student)
            redirect('', 'refresh');
    }

    function _get_event_data($event) {
        $data = array(
            'id' => $event->id,
            'author_id' => $event->user_id,
            'author' => $this->user_profiles_model->get_user_fullname($event->user_id),
            'creation_date' => date('d/m/Y', strtotime($event->created)),
            'creation_time' => date('H:i:s', strtotime($event->created)),
            'title' => $event->title,
            'content' => $event->content
        );

        if(isset($event->last_update)) {
            $data['last_update_date'] = date('d/m/Y', strtotime($event->last_update));
            $data['last_update_time'] = date('H:i:s', strtotime($event->last_update));
            $data['updated'] = true;
        } else
            $data['updated'] = false;

        if(isset($event->published)) {
            $data['publish_date'] = date('d/m/Y', strtotime($event->published));
            $data['publish_time'] = date('H:i:s', strtotime($event->published));
            $data['published'] = true;
        } else
            $data['published'] = false;

        return $data;
    }

    function index($tab=null) {
        if($this->input->post('event_submit') !== false) {

            $this->form_validation->set_rules('event_title', 'lang:event_title', 'trim|required');
            $this->form_validation->set_rules('event_content', 'lang:event_content', 'trim|required');

            if($this->form_validation->run() !== false) {
                if($this->input->post('event_id') !== false)
                    $this->events_model->update($this->input->post('event_id'), $this->input->post('event_title'), $this->input->post('event_content'));
                else
                    $this->events_model->create($this->input->post('event_title'), $this->input->post('event_content'));
            }
        }

        if($this->is_student) {
            if(!isset($tab))
                $tab = 'list';

            $class_id = $this->students_model->get_by_id($this->tank_auth->get_user_id())->class_id;
            $events = $this->events_model->get_by_class($class_id);
            $tabs[$tab]['events'] = array();

            foreach($events as $event)
                $tabs[$tab]['events'][$event->id] = $this->_get_event_data($event);
        }

        if($this->is_admin) {
            if(!isset($tab))
                $tab = 'admin';

            $events = $this->events_model->get_by_user($this->tank_auth->get_user_id());

            $tabs[$tab]['events_by_user'] = array();

            foreach($events as $event)
                $tabs[$tab]['events_by_user'][$event->id] = $this->_get_event_data($event);

            if($this->tank_auth->is_role('teacher')) {
                $classes = $this->classes_model->get_by_teacher($this->tank_auth->get_user_id());
                $tabs[$tab]['classes'] = array();

                foreach($classes as $class) {
                    $events = $this->events_model->get_by_user_and_class($this->tank_auth->get_user_id(), $class->id);
                    $tabs[$tab]['classes'][$class->id] = array();

                    foreach($events as $event)
                        $tabs[$tab]['classes'][$class->id][$event->id] = $this->_get_event_data($event);
                }
            } else {
                $class_id = $this->students_model->get_by_id($this->tank_auth->get_user_id())->class_id;
                $events = $this->events_model->get_by_user_and_class($this->tank_auth->get_user_id(), $class_id);

                foreach($events as $event)
                    $tabs[$tab]['classes'][$class_id] = $this->_get_event_data($event);
            }
        }

        $data['tabs'] = $tabs;
        $data['tab_active'] = $tab;
        $this->ingesup->render('events/index', $data);
    }

    function view($event_id) {
        $data['event'] = $this->_get_event_data($this->events_model->get_by_id($event_id));
        $this->ingesup->render('events/view', $data);
    }

    function edit($event_id=null) {
        if(isset($event_id))
            $data['event'] = $this->_get_event_data($this->events_model->get_by_id($event_id));
        else
            $data = '';
        $this->ingesup->render('events/edit', $data);
    }

    function delete($event_id) {
        $this->events_model->delete($event_id);
        $this->index('admin');
    }

    function link($class_id, $event_id) {
        $this->events_model->link($class_id, $event_id);
        $this->index('admin');
    }

    function unlink($class_id, $event_id) {
        $this->events_model->unlink($class_id, $event_id);
        $this->index('admin');
    }

    function show($class_id, $event_id) {
        $this->events_model->show($class_id, $event_id);
        $this->index('admin');
    }

    function mask($class_id, $event_id) {
        $this->events_model->mask($class_id, $event_id);
        $this->index('admin');
    }
}